layui.config({
    base: '/static/js/lib/',
    version: new Date().getTime()
}).use(['layer', "table", "laytpl", "laydate"], function () {

    $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var laytpl = layui.laytpl;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#beginTime' //指定元素
        , type: 'datetime'
    });

    $('.select').click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        $.post("/phases/list", {parentId: $(this).attr("value")}, function (res) {
            if (res.code == "00000") {
                var phases = res.data;
                var html = "";
                for (var i = 0; i < phases.length; i++) {
                    html += '<a class="layui-btn phasesSelect" value="' + phases[i].id + '">' + phases[i].name + '</a>';
                }
                $('.phasesUl').html(html);
                // 下拉框选择
                $('.phasesSelect').click(function () {
                    $(this).addClass("active").siblings().removeClass("active");
                });
            } else {
                console.log(res);
            }
        });
    });


    $('.save').click(function () {
        var html = "<tr class='tr'>";
        $($('.scope th')).each(function () {
            var id = $(this).attr("id");
            if (id != 0) {
                html += '<td id="' + id + '"><input type="text" placeholder="1-15" class="layui-input">'
            } else {
                html += '<td id="0"><a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger remove" >删除</a>'
            }
        });
        html += '</tr>';
        $('.scheme').append(html);

        $('.remove').click(function () {
            var tr = $(this).parents('tr');
            layer.confirm("是否删除", {icon: 0, title: '提示'}, function () {
                layer.msg("删除成功");
                tr.remove();
            });

        });

    });

    $('.add').click(function () {
        var rule = [];
        $('.tr').each(function () {
            $('.tr').each(function () {
                var td = [];
                $($(this).find('td')).each(function () {
                    var _this = $(this);
                    var id = _this.attr("id");
                    var value = _this.find('input').val();
                    if (id != 0) {
                        var data = {id: id, value: value};
                        td.push(data)
                    }
                });
                rule.push(td);
            });
            console.log(JSON.stringify(rule))
        });
        $.post("/assessment/update", {
            assessmentId: $('#assessmentId').val(),
            rule: JSON.stringify(rule)
        }, function (res) {
            if (res.code == "00000") {
                var phases = res.data;
                var html = "";
                for (var i = 0; i < phases.length; i++) {
                    html += '<a class="layui-btn phasesSelect" value="' + phases[i].id + '">' + phases[i].name + '</a>';
                }
                $('.phasesUl').html(html);
                // 下拉框选择
                $('.phasesSelect').click(function () {
                    $(this).addClass("active").siblings().removeClass("active");
                });
            } else {
                console.log(res);
            }
        });
    });


    $('.random').click(function () {
        layer.open({
            type: 2,
            area: ['100%', '100%'],
            fix: false,
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: "抽选规则",
            content: "/random?assessmentId=" + $(this).attr("id")
        });
    })

});